// 封装一个栈类

function Stack() {
    // 属性
    this.items = [];
    // 压栈
    Stack.prototype.push = function(element) {
        this.items.push(element);
    };

    // 弹栈
    Stack.prototype.pop = function() {
        return this.items.pop();
    };
    // 返回栈顶元素
    Stack.prototype.peek = function() {
        return this.items[this.items.length - 1];
    };
    // 判断是否为空
    Stack.prototype.isEmpty = function() {
        return this.items.length === 0;
    };
    // 判断大小
    Stack.prototype.size = function() {
        return this.items.length;
    };
    // 返回字符形式内容
    Stack.prototype.toString = function() {
        let toStr = '';
        for (let i = 0; i < this.items.length; i++) {
            toStr += this.items[i];
        }
        return toStr;
    };
}

// 使用栈
const stack = new Stack();

stack.push(1);
stack.push(2);
stack.push(3);
stack.push(7);
stack.push(8);
console.log(stack.pop());
console.log(stack.peek());
console.log(stack.isEmpty());
console.log(stack.size());
console.log(stack.toString());

function bin2dec(num) {
    const myStack = new Stack();

    while (num > 0) {
        myStack.push(num % 2);
        num = Math.floor(num / 2);
    }
    let bstring = '';
    while (!myStack.isEmpty()) {
        bstring += myStack.pop();
    }
    return bstring;
}

console.log(bin2dec(10));